<template>
  <div class="view">
   <img src="https://klcx-bucket.oss-cn-beijing.aliyuncs.com/klcx-xcx/ico/img3.png" class="img3"/>
    <div style="text-align: center">
      <h1>￥{{cashNum}}</h1>
      <text class="tip1">押金随时可退</text>
    </div>
    <button class="btn pass fix" @click="payment()" :disabled="disable">立即支付</button>
  </div>
</template>

<script>
  import Service from '@/utils/service'

  export default {
    data () {
      return {
        cashNum:null,
        disable:false
      }
    },
    onUnload() {
      setTimeout(() => {
        wx.reLaunch({
          url: '../index/main'
        })
      },200)
    },
    onLoad(options) {
//      this.cashNum = options.rechargeDepositAmount
    },
    onShow(){
      this.getCash()
    },
    methods: {
      getCash(){
        let data = {
          userId:wx.getStorageSync('userInfo').userId
        }
        Service.getwallet(data).then((res)=>{
          if (res.code == 200) {
            this.cashNum = res.data.rechargeDepositAmount
          }
        })
      },
      payment() {
        let _this = this
        this.disable = true
        setTimeout(() => {
          _this.disable = false
        },2000)
        wx.login({
          success: res => {
            // 发送 res.code 到后台换取 openId, sessionKey, unionId
            if (res.code) {
              let data = {code: res.code}
              Service.jscode2session(data).then((res) => {
                if (res) {
                  let adCode = wx.getStorageSync('adCode')
                  let data = {
                    payMethod: '1',//微信
                    paymentType: '19',//押金
                    adCode: adCode,
                    openId: res.openid
                  }
                  Service.prePay(data).then((res) => {
                    if (res.code == 200) {
                      let payParam = res.data
                      wx.requestPayment({
                        timeStamp: res.data.timeStamp +'',
                        nonceStr: res.data.nonceStr+'',
                        package: res.data.package+'',
                        signType: 'MD5',
                        paySign: res.data.sign+'',
                        success(res) {
                          wx.showToast({
                            title: '支付成功',
                            icon: 'success',
                            duration: 2000
                          });
                          setTimeout(()=> {
                            wx.reLaunch({
                              url: '../success/main'
                            })
                          },200)
                        },
                        fail(res) {
                          _this.disable = false
                        }
                      })
                    }
                  })
                }
              })
            }
          }
        })

      }
    }
  }
</script>

<style scoped>
.view{
  padding: 50px;
  text-align: center;
}
.tip1{
  font-size:30rpx;
  font-family:PingFangSC-Regular;
  font-weight:400;
  color:rgba(153,153,153,1);
  line-height:42rpx;
}
  .img3{
    width: 176rpx;
    height: 152rpx;
    margin-top: 253rpx;
    margin-bottom: 36rpx;
  }
  h1{
    line-height: 100rpx;
    font-size: 72rpx;
    margin-bottom: 0;
  }

</style>
